from PyQt4 import QtCore, QtGui

from rasterdialog import Ui_RasterAlgebra

import os

class RasterDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        # Set up the user interface from Designer.
        self.ui = Ui_RasterAlgebra()
        self.ui.setupUi(self)

        # click the file button and get a file dialog:
        self.connect(self.ui.fileBrowse, QtCore.SIGNAL("clicked()"),
                     self.browser)

        # update the expression text:
        self.connect(self.ui.layer1, QtCore.SIGNAL("currentIndexChanged(int)"),self.changeL1)
        self.connect(self.ui.layer2, QtCore.SIGNAL("currentIndexChanged(int)"),self.changeL2)

        self.connect(self.ui.func1, QtCore.SIGNAL("currentIndexChanged(int)"),self.updateExp)
        self.connect(self.ui.func2, QtCore.SIGNAL("currentIndexChanged(int)"),self.updateExp)

        self.connect(self.ui.op, QtCore.SIGNAL("currentIndexChanged(int)"),self.updateExp)

        # if you start typing in the constant boxes, set the constant flag on:
        self.connect(self.ui.constant1, QtCore.SIGNAL("textChanged(QString)"),self.changeC1)
        self.connect(self.ui.constant2, QtCore.SIGNAL("textChanged(QString)"),self.changeC2)

        # constants must be doubles:
        self.ui.constant1.setValidator(QtGui.QDoubleValidator(self.ui.constant1))
        self.ui.constant2.setValidator(QtGui.QDoubleValidator(self.ui.constant2))

        self.connect(self.ui.useLayer1, QtCore.SIGNAL("toggled(bool)"),self.updateExp)
        self.connect(self.ui.useLayer2, QtCore.SIGNAL("toggled(bool)"),self.updateExp)

        self.connect(self.ui.useConstant1, QtCore.SIGNAL("toggled(bool)"),self.updateExp)
        self.connect(self.ui.useConstant2, QtCore.SIGNAL("toggled(bool)"),self.updateExp)

        self.connect(self.ui.outFileName, QtCore.SIGNAL("textChanged(QString)"),self.updateExp)

        # start with the layers chosen:
        self.ui.useLayer1.setChecked(True)
        self.ui.useLayer2.setChecked(True)

    def changeC1(self):
        print "C1 changed"
        self.ui.useLayer1.setChecked(False)
        self.ui.useConstant1.setChecked(True)
        self.updateExp()

    def changeC2(self):
        self.ui.useLayer2.setChecked(False)
        self.ui.useConstant2.setChecked(True)
        self.updateExp()

    def changeL1(self):
        print "l1 changed"
        self.ui.useLayer1.setChecked(True)
        self.ui.useConstant1.setChecked(False)
        self.updateExp()

    def changeL2(self):
        self.ui.useLayer2.setChecked(True)
        self.ui.useConstant2.setChecked(False)
        self.updateExp()
        

    @QtCore.pyqtSignature("int")
    def updateExp(self):
        self.ui.expression.setText(self.computeExpression())
        return None
        
    def computeExpression(self):
        res=os.path.basename(str(self.ui.outFileName.text()))
        f1=str(self.ui.func1.currentText())
        l1=str(self.ui.layer1.currentText())
        c1=str(self.ui.constant1.text())

        if self.ui.useConstant1.isChecked():
            l1=c1
        
        op=str(self.ui.op.currentText())
        
        f2=str(self.ui.func2.currentText())
        l2=str(self.ui.layer2.currentText())
        c2=str(self.ui.constant2.text())

        if self.ui.useConstant2.isChecked():
            l2=c2

        b1=("(",")")
        if f1.strip() == "":
            b1=("","")
        b2=("(",")")
        if f2.strip() == "":
            b2=("","")

        return "%s = %s%s%s%s %s %s%s%s%s" % (res,f1,b1[0],l1,b1[1],op,f2,b2[0],l2,b2[1])

    def browser(self):
        print "file browsing..."
        qd=QtGui.QFileDialog()
        f=qd.getSaveFileName()
        if len(f) > 0:
            self.ui.outFileName.setText(f)

